<template>
  <div>
    <!-- 账期新增 -->
    <el-dialog
      width="500px"
      custom-class="customWidth"
      center
      :before-close="closeDialog"
      title="账期新增"
      :close-on-click-modal="false"
      :visible.sync="DialogFormVisible"
    >
      <el-form
        :model="adddataForm"
        :rules="addRule"
        ref="adddataForm"
        @keyup.enter.native="addataFormSubmit()"
        label-width="80px"
      >
        <el-form-item label="账期名称" prop="paymentName">
          <el-col :span="15">
            <el-input v-model="adddataForm.paymentName" size="mini"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="开始日期" prop="startTime">
          <el-col :span="15">
            <el-date-picker
              size="mini"
              widtd="100%"
              v-model="adddataForm.startTime"
              type="date"
              value-format="yyyy-MM-dd"
              :picker-options="pickerOptions"
            >
            </el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="结束日期" prop="endTime">
          <el-col :span="15">
            <el-date-picker
              size="mini"
              widtd="100%"
              v-model="adddataForm.endTime"
              type="date"
              value-format="yyyy-MM-dd"
              :picker-options="pickerOptions"
            >
            </el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="所属年份" prop="year">
          <el-col :span="15">
            <el-date-picker
              size="mini"
              v-model="adddataForm.year"
              type="year"
              value-format="yyyy"
              :picker-options="pickerOptions"
            >
            </el-date-picker>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible()">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      </span>
    </el-dialog>
    <!-- 账期修改 -->
    <el-dialog
      width="500px"
      custom-class="customWidth"
      center
      :before-close="eidtcloseDialog"
      title="账期修改"
      :close-on-click-modal="false"
      :visible.sync="eidDialogFormVisible"
    >
      <el-form
        :model="eidtdataForm"
        :rules="eidtRule"
        ref="eidtdataForm"
        @keyup.enter.native="addataFormSubmit()"
        label-width="80px"
      >
        <el-form-item label="账期名称" prop="paymentName">
          <el-col :span="15">
            <el-input v-model="eidtdataForm.paymentName" size="mini"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="开始日期" prop="startTime">
          <el-col :span="15">
            <el-date-picker
              size="mini"
              widtd="100%"
              v-model="eidtdataForm.startTime"
              type="date"
              value-format="yyyy-MM-dd"
              :picker-options="pickerOptions"
            >
            </el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="结束日期" prop="endTime">
          <el-col :span="15">
            <el-date-picker
              size="mini"
              widtd="100%"
              v-model="eidtdataForm.endTime"
              type="date"
              value-format="yyyy-MM-dd"
              :picker-options="pickerOptions"
            >
            </el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="所属年份" prop="year">
          <el-col :span="15">
            <el-date-picker
              size="mini"
              v-model="eidtdataForm.year"
              type="year"
              value-format="yyyy"
              :picker-options="pickerOptions"
            >
            </el-date-picker>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="eidtvisible()">取消</el-button>
        <el-button type="primary" @click="eidtdataFormSubmit()">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      adddataForm: {
        paymentName: "",
        startTime: "",
        endTime: "",
        year: ""
      },
      eidtdataForm: {
        paymentName: "",
        startTime: "",
        endTime: "",
        year: "",
        unitGuid: "",
        paymentGuid: ""
      },
      DialogFormVisible: false,
      eidDialogFormVisible: false,
      addRule: {},
      eidtRule: {},
      pickerOptions: {
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            }
          }
        ]
      }
    };
  },
  created(){
    this.$set(this.adddataForm,"startTime",this.getNow(0,'yy-mm-dd'))
    this.$set(this.adddataForm,"endTime",this.getNow(31,'yy-mm-dd'))
  },
  computed: {
    changeschoolid: {
      get() {
        return this.$store.state.user.changeschoolid;
      }
    }
  },
  methods: {
    addinit() {
      this.DialogFormVisible = true;
    },
    closeDialog(done) {
      this.$refs["adddataForm"].resetFields();
      done();
    },
    visible() {
      this.$refs["adddataForm"].resetFields();
      this.DialogFormVisible = false;
    },
    dataFormSubmit() {
      this.$http({
        url: this.$http.adornUrl("/terminal/consume/payment-day-add"),
        method: "post",
        data: this.$http.adornData({
          messageContent: {
            endTime: this.adddataForm.endTime,
            paymentName: this.adddataForm.paymentName,
            startTime: this.adddataForm.startTime,
            status: 0,
            year: this.adddataForm.year,
            unitGuid: this.changeschoolid
          },
          messageType: 0
        })
      }).then(({ data }) => {
        console.log(data);
        if (data.messageCode == 200) {
          this.$message({
            type: "success",
            message: "新增账期成功!",
            duration: 200,
            onClose: () => {
              this.$emit("refreshDataList");
              this.DialogFormVisible = false;
            }
          });
        } else {
          this.$message.error(data.errorMessage);
        }
      });
    },

    //编辑
    eidtinit(row) {
      console.log(row);
      this.eidtdataForm.unitGuid = row.unitGuid;
      this.eidtdataForm.paymentName = row.paymentName;
      this.eidtdataForm.startTime = row.startTime;
      this.eidtdataForm.endTime = row.endTime;
      this.eidtdataForm.paymentGuid = row.paymentGuid;
      this.eidtdataForm.year = row.year.toString();
      this.eidDialogFormVisible = true;
    },
    eidtcloseDialog(done) {
      this.$refs["eidtdataForm"].resetFields();
      done();
    },
    eidtvisible() {
      this.$refs["eidtdataForm"].resetFields();
      this.eidDialogFormVisible = false;
    },
    eidtdataFormSubmit() {
      this.$http({
        url: this.$http.adornUrl("/terminal/consume/payment-day-update"),
        method: "post",
        data: this.$http.adornData({
          messageContent: {
            endTime: this.eidtdataForm.endTime,
            paymentName: this.eidtdataForm.paymentName,
            startTime: this.eidtdataForm.startTime,
            status: 0,
            year: this.eidtdataForm.year,
            unitGuid: this.eidtdataForm.unitGuid,
            paymentGuid: this.eidtdataForm.paymentGuid
          },
          messageType: 0
        })
      }).then(({ data }) => {
        console.log(data);
        if (data.messageCode == 200) {
          this.$message({
            type: "success",
            message: "修改账期成功!",
            duration: 200,
            onClose: () => {
              this.$emit("refreshDataList");
              this.eidDialogFormVisible = false;
            }
          });
        } else {
          this.$message.error(data.errorMessage);
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}
</style>
